﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PythonInvokerTest.Utils
{
    public class FileTreeBuilder
    {
        static bool addFileToTree = false;

        /// <summary>
        /// 根据输入路径创建树型目录。
        /// </summary>
        /// <param name="treeView">待显示树的目录。</param>
        /// <param name="path">待处理路径。</param>
        public static void BuildTreeView(TreeView treeView, string path)
        {
            treeView.Nodes.Clear(); // 只显示1个根目录
            var rootNode = treeView.Nodes.Add(Path.GetFileName(path), Path.GetFileName(path));
            rootNode.Tag = path;
            rootNode.ImageIndex = 1;
            AddDirectories(rootNode, path);
            CountPythonScripts(rootNode);
            rootNode.Expand();
        }

        /// <summary>
        /// 根据路径信息添加节点目录。
        /// </summary>
        /// <param name="parentNode">待添加脚本的节点。</param>
        /// <param name="directory"></param>
        private static void AddDirectories(TreeNode parentNode, string directory)
        {
            if (!Directory.Exists(directory))
                return;

            // 添加目录
            foreach (var subDir in Directory.GetDirectories(directory))
            {
                var node = parentNode.Nodes.Add(Path.GetFileName(subDir));
                node.Tag = subDir;
                node.ImageIndex = 1; // 
                AddDirectories(node, subDir);
            }

            // 添加文件
            if (addFileToTree)
            {
                foreach (var fullpath in Directory.GetFiles(directory))
                {
                    var last_path_name = Path.GetFileName(fullpath);
                    if (!last_path_name.ToLower().EndsWith(".py"))
                        Console.WriteLine();

                    var node = new TreeNode(last_path_name) { Tag = fullpath, ImageIndex = 2 };
                    parentNode.Nodes.Add(node);

                    var icon = GetIconFromFile(last_path_name);
                    if (icon != null)
                    {
                        node.TreeView.ImageList.Images.Add(icon);
                        node.ImageIndex = node.TreeView.ImageList.Images.Count - 1;
                    }
                }
            }
        }


        /// <summary>
        /// 2024/05/30 测试可用。参考：https://blog.csdn.net/lzl_li/article/details/117038323
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static System.Drawing.Icon GetIconFromFile(string fileName)
        {
            return File.Exists(fileName) ? System.Drawing.Icon.ExtractAssociatedIcon(fileName) : null;
        }


        /// <summary>
        /// 通过递归遍历的方式，统计指定节点的目录及所有子目录的Python脚本数量。
        /// </summary>
        /// <param name="parentNode">目标节点。</param>
        /// <returns></returns>
        private static int CountPythonScripts(TreeNode parentNode)
        {
            int total = 0;

            for (int i = parentNode.Nodes.Count - 1; i >= 0; i--)
            {
                TreeNode node = parentNode.Nodes[i];
                if (node.Tag is string path)
                {
                    if (path.ToLower().EndsWith(".py"))
                        total++;
                    else
                        total += CountPythonScripts(node);
                }
            }
            //if (total == 0 && parentNode.Parent != null)
            //    parentNode.Parent.Nodes.Remove(parentNode);

            return total;
        }
    }
}
